package com.example.kotlinview.flow

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import java.lang.Thread.sleep

fun numbers(): Flow<Int> = flow {
    try {
        emit(1)
        emit(2)
        println("This line will not execute")
        emit(3)
    } finally {
        println("Finally in numbers")
    }
}.flowOn(Dispatchers.IO)

fun main() = runBlocking<Unit> {
    numbers().take(2)
        .filter {
            println("Filter $it")
            it % 2 == 0
        }.map {
            println("Map $it")
            "string $it"
        }
        .collect {
            println("Collect $it")
        }
}